package com.leyou.order.service;

import com.leyou.common.utils.IdWorker;
import com.leyou.common.vo.PageResult;
import com.leyou.entity.UserInfo;
import com.leyou.order.client.GoodsClient;
import com.leyou.order.dto.OrderDto;
import com.leyou.order.interceptor.UserInterceptor;
import com.leyou.order.mapper.OrderDetailMapper;
import com.leyou.order.mapper.OrderMapper;
import com.leyou.order.mapper.OrderStatusMapper;
import com.leyou.order.pojo.Order;
import com.leyou.order.utils.PayHelper;
import com.leyou.pojo.Spu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.Map;


@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private PayHelper payHelper;

    @Autowired
    private GoodsClient goodsClient;

    @Transactional
    public Long createOrder(OrderDto orderDto) {
        Order order = new Order();
        long id = idWorker.nextId();
        order.setOrderId(id);
        order.setCreateTime(new Date());
       // order.setPaymentType(orderDto.getPaymentType());

/*        UserInfo user = UserInterceptor.getUser();
        order.setUserId(user.getId());
        order.setBuyerNick(user.getName());*/
        order.setBuyerRate(false);
        order.setTotalPay(10000l);

        order.setActualPay(1024l);
        orderMapper.insertSelective(order);
        goodsClient.decrease(id);
        return id;
    }

    public String createPayUrl(Long id) {
        return payHelper.createPayUrl(id,"爱华",1l);
    }

    public void handlerNotify(Map<String, String> result) {
        payHelper.isSuccess(result);

        //检验签名
        payHelper.handleNotify(result);
    }

    public Integer queryOrderState(Long id) {
        return  payHelper.queryPayState(id).getValue();
    }
}
